﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.GameEventMechanics {
    import Engine.Managers.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Events.*;
    import Classes.*;
    import Display.*;
    import Classes.doobers.*;
    import Mechanics.Transactions.*;

    public class StorageStateHarvestMechanic implements IActionGameMechanic, IMultiPickSupporter, IToolTipModifier {

        protected var m_owner:MechanicMapResource;
        protected var m_config:MechanicConfigData;
        protected var m_doobers:Array;
        protected var m_gameEvent:String = "all";

        public function StorageStateHarvestMechanic(){
            this.m_doobers = new Array();
            super();
        }
        public function hasOverrideForGameAction(_arg1:String):Boolean{
            var _local3:int;
            var _local2:Boolean;
            this.m_gameEvent = _arg1;
            if (!this.m_owner.isNeedingRoad){
                _local3 = this.getRequiredEnergy();
                if ((_local3 > 0)){
                    _local2 = true;
                };
            };
            return (_local2);
        }
        public function executeOverrideForGameEvent(_arg1:String, _arg2:Array=null):MechanicActionResult{
            var _local3:Object = this.m_owner.getDataForMechanic(this.m_config.type);
            var _local4:Boolean;
            var _local5:Boolean;
            if ("true" == this.m_config.params["blockOthers"]){
                _local5 = true;
            };
            var _local6:String;
            var _local7:int = this.getRequiredEnergy();
            if (Global.player.checkEnergy((-1 * _local7))){
                _local4 = true;
                _local6 = ZLoc.t("Dialogs", "HarvestConfirmMechanic", {
                    building:this.m_owner.getItemFriendlyName(),
                    cost:_local7
                });
                UI.displayMessage(_local6, GenericPopup.TYPE_YESNO, this.onHarvestConfirm, "", true);
            } else {
                _local6 = ZLoc.t("Main", "NotEnoughEnergyRed");
                this.m_owner.displayStatus(_local6);
            };
            return (new MechanicActionResult(_local4, !(_local5), false, null));
        }
        protected function preHarvest(_arg1:GenericPopupEvent):void{
        }
        protected function postHarvest(_arg1:GenericPopupEvent):void{
        }
        protected function onHarvestConfirm(_arg1:GenericPopupEvent):void{
            var _local2:int;
            var _local3:Item;
            var _local4:Object;
            var _local5:Array;
            var _local6:String;
            var _local7:Array;
            var _local8:int;
            var _local9:Array;
            if (_arg1.button == GenericPopup.ACCEPT){
                this.preHarvest(_arg1);
                _local2 = this.getRequiredEnergy();
                if (Global.player.updateEnergy((-1 * _local2), [])){
                    _local4 = this.m_owner.getDataForMechanic(this.m_config.type);
                    _local5 = (_local4["resources"] as Array);
                    for each (_local6 in _local5) {
                        _local3 = Global.gameSettings.getItemByName(_local6);
                        _local7 = Global.player.processRandomModifiers(_local3, this.m_owner, true, []);
                        _local8 = 0;
                        while (_local8 < _local7.length) {
                            _local9 = _local7[_local8];
                            if (_local9[0] == Global.gameSettings.getDooberFromType(Doober.DOOBER_COIN, _local9[1])){
                                (_local7[_local8] as Array)[1] = (_local9[1] * _local3.commodityReq);
                            };
                            _local8++;
                        };
                        this.m_doobers = this.m_doobers.concat(_local7);
                    };
                    Global.world.dooberManager.createBatchDoobers(this.m_doobers, this.m_owner.getItem(), this.m_owner.getPosition().x, this.m_owner.getPosition().y, false, this.onDoobersCollected);
                    this.m_owner.setDataForMechanic(this.m_config.type, null);
                    TransactionManager.addTransaction(new TMechanicAction(this.m_owner, this.m_config.type, this.m_gameEvent, {operation:"harvest"}));
                    this.postHarvest(_arg1);
                };
            };
        }
        protected function onDoobersCollected():void{
            this.m_doobers = null;
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_config = _arg2;
        }
        public function getPick():String{
            return (this.m_config.params["pick"]);
        }
        public function isVisitEnabled():Boolean{
            return (false);
        }
        public function getToolTipStatus():String{
            var _local3:int;
            var _local4:int;
            var _local1:String;
            var _local2:Object = this.m_owner.getDataForMechanic(this.m_config.type);
            if (_local2){
                if (((((!((_local2["customers"] == null))) && (!((_local2["customersReq"] == null))))) && (_local2["resources"]))){
                    _local3 = _local2["customers"];
                    _local4 = _local2["customersReq"];
                    _local1 = ZLoc.t("Main", "BusinessCustomers", {
                        served:_local3,
                        max:_local4
                    });
                };
            };
            return (_local1);
        }
        public function getToolTipAction():String{
            var _local1:String;
            var _local2:int = this.getRequiredEnergy();
            if (_local2 > 0){
                if (Global.player.checkEnergy((-1 * _local2), false)){
                    _local1 = ZLoc.t("Main", "BusinessIsHarvestableNoAmount");
                } else {
                    _local1 = ZLoc.t("Main", "NeedEnergy", {amount:_local2});
                };
            };
            return (_local1);
        }
        protected function getRequiredEnergy():int{
            var _local3:Item;
            var _local4:Array;
            var _local5:String;
            var _local1 = -1;
            var _local2:Object = this.m_owner.getDataForMechanic(this.m_config.type);
            if (_local2){
                if (((((_local2["customers"]) && (_local2["customersReq"]))) && (_local2["resources"]))){
                    if (_local2["customers"] >= _local2["customersReq"]){
                        _local1 = 0;
                        _local3 = null;
                        _local4 = (_local2["resources"] as Array);
                        for each (_local5 in _local4) {
                            _local3 = Global.gameSettings.getItemByName(_local5);
                            _local1 = (_local1 + _local3.harvestEnergyCost);
                        };
                    };
                };
            };
            return (_local1);
        }

    }
}//package Mechanics.GameEventMechanics 
